package com.weilele.mvvmlib.sample.fg

import com.weilele.base.library.BaseViewModel
import com.weilele.mvvm.base.helper.*

class MeiziViewModel : BaseViewModel() {

    val pageData = pageHelperData<MeiziBean.DataDTO>()

    fun getPic(isRefresh: Boolean) {
        val pageHelper = pageData.data
        if (pageHelper != null) {
            //不是第一次请求数据
            if (!pageHelper.hasNextPage) {
                return
            }
        }
        if (pageData.isRunning) {
            return
        }
        val pageIndex = if (isRefresh) {
            1
        } else {
            (pageHelper?.currentPageIndex ?: 1) + 1
        }
        pageData.bindNetwork(isKeepLastValue = true) {
            val result = TestApi.self.getMeizi(pageIndex, 20)
            pageData.updatePageHelper(
                pageIndex,
                result.data,
                isRefresh,
                null,
                result.totalCounts,
                result
            )
        }
    }
}